/******************************************************************************
* stability of financial distress outcomes between 2001 and 2015
*****************************************************************************/

*****************************************************************************
**  load TU data from spark server
*****************************************************************************

*set up globals based on geo level
geoLevel


foreach y in 2001 2015{
	* 6/2001 cz level 
	import delimited using "${dataRoot}/sumstats_`y'${sumStatsM}_${Geo}.csv", ///
		delimiters(",") varnames(1) numericcols(${numCol}/99) clear
		
	cleanUpVars ${GeoCZ}

	save "${dataRoot}/TU_Debt_by_${Geo}_`y'.dta", replace
}

*****************************************************************************
**  plot rank stability and correlations in different years (Figure A6,A7)
*****************************************************************************

** append three tables
use          "${dataRoot}/TU_Debt_by_${Geo}_2015.dta", clear
append using "${dataRoot}/TU_Debt_by_${Geo}_2001.dta"

keep if ${Geo} !=.
tab year

*reshape to bring each year to columns
drop asofdate

keep ${Geo} year numobs $keyMvVarsMain
reshape wide numobs $keyMvVarsMain, i(${Geo}) j(year)


*years to compare
local yYear = 2015
local xYear = 2001


foreach var in $keyMvVarsMain {
	*scatter plot of outcome variable rank yYear vs xYear, no controls
	preserve
		
		keep if `var'`yYear'>0 & `var'`xYear'>0
		
		sort `var'`yYear'
		gen rank_`yYear' = _n 
		sort `var'`xYear'
		gen rank_`xYear' = _n
		
		*show slope coefficient
		reg rank_`yYear' rank_`xYear' 
		
		figShowSlope rank_`yYear' rank_`xYear' 
		
		*plot scatter
		twoway	(scatter rank_`yYear' rank_`xYear',	  msize(small))  ///
				(lfit rank_`xYear' rank_`xYear', lwidth(thick) lcolor(maroon)) ///
				, ytitle("`yYear'", size(medlarge) margin(vsmall)) ///
				  xtitle("`xYear'", size(medlarge) margin(vsmall)) ///
				  graphregion(fcolor(white)) legend(off) ///
				  title("${figShowSlope}", position(${betaPos}) ring(0) size(medium)) 
				
		graph export "${figGeoDebt}/rk`yYear'v`xYear'_`var'.png", replace		
	restore
	

	
	*scatter plots of normalized outcome variables
	preserve
		qui replace `var'`yYear'=. if `var'`xYear'==.
		qui sum `var'`yYear' 
		qui replace `var'`yYear' = (`var'`yYear'-`r(mean)')/`r(sd)'
		
		
		qui replace `var'`xYear'=. if `var'`yYear'==.
		qui sum `var'`xYear' 
		qui replace `var'`xYear' = (`var'`xYear'-`r(mean)')/`r(sd)'
		
		reg `var'`yYear' `var'`xYear' 
		figShowSlope `var'`yYear' `var'`xYear' "" "" "" "correlation"
		
		corr  `var'`yYear' `var'`xYear' 
		
		*winsorize
		sum `var'`xYear' , d
		replace `var'`xYear'=. if `var'`xYear' >`r(p99)'
		replace `var'`xYear'=. if `var'`xYear' <`r(p1)'
		sum `var'`xYear' 
		local xlb = ceil(`r(min)')
		local xub = floor(`r(max)')
		
		sum `var'`yYear' , d
		replace `var'`yYear'=. if `var'`yYear' >`r(p99)'
		replace `var'`yYear'=. if `var'`yYear' <`r(p1)'
		sum `var'`yYear' 			
		local ylb = ceil(`r(min)')
		local yub = floor(`r(max)')
		
		*plot scatter
		twoway	(scatter `var'`yYear' `var'`xYear',	  msize(small))  ///
				(lfit `var'`xYear' `var'`xYear', lwidth(thick) lcolor(maroon)) ///
				, ytitle("`yYear'", size(medlarge) margin(vsmall)) ///
				  xtitle("`xYear'", size(medlarge) margin(vsmall)) ///
				  xscale(range(`xlb'(1)`xub')) xlabel(`xlb'(1)`xub') ///
				  yscale(range(`ylb'(1)`yub')) ylabel(`ylb'(1)`yub') ///
				  graphregion(fcolor(white)) legend(off) ///
				  title("${figShowSlope}", position(${betaPos}) ring(0) size(medium)) 
				
		graph export "${figGeoDebt}/rk`yYear'v`xYear'_nrml_nonrank_`var'.png", replace	
	restore
}

*close all graph windows
window manage close graph _all
